using Nb.LMS.Applications.Accounts.Abstracts;
using Nb.LMS.Domains.Accounts.Abstracts;

namespace Nb.LMS.Applications.Accounts
{
    public class AuthenticateService : IAuthenticateService
    {
        private readonly IAccountRepository _accountRepository;
        private readonly IAccountPolicyService _accountPolicyService;

        public AuthenticateService(IAccountRepository accountRepository, IAccountPolicyService accountPolicyService)
        {
            _accountRepository = accountRepository;
            _accountPolicyService = accountPolicyService;
        }

        public bool Validate(string login, string password)
        {
            var account = _accountRepository.GetByUserLogin(login);
            if (account == null)
            {
                return false;
            }

            var encryptPassword = _accountPolicyService.EncryptPassword(password, account.PasswordFormat, account.PasswordSalt);
            return encryptPassword == password;
        }
    }
}